package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.PageInfo;
import com.example.reggie.common.R;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.service.CategoryServcie;
import com.example.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryServcie categoryServcie;
@PostMapping
     public R<String> addSetmeal(@RequestBody SetmealDto setmealDto)
{
       setmealService.saveWithDish(setmealDto);
       return  R.success("套餐新增成功");
}

  @GetMapping("/page")
    public R<Page> getSetmealListWithPage(PageInfo pageInfo)
  {
      Page<Setmeal> setmealPage = new Page<>(pageInfo.getPage(), pageInfo.getPageSize());
      Page<SetmealDto> setmealDtoPage = new Page<>(pageInfo.getPage(),pageInfo.getPageSize());
      LambdaQueryWrapper<Setmeal> setmealQuery = new LambdaQueryWrapper<>();
      setmealQuery.like(pageInfo.getName()!=null,Setmeal::getName,pageInfo.getName())
                .orderByDesc(Setmeal::getUpdateTime);
      setmealService.page(setmealPage,setmealQuery);

      BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");


      List<SetmealDto> setmealDtoList = setmealPage.getRecords().stream().map(item -> {
              SetmealDto setmealDto = new SetmealDto();
          Category category = categoryServcie.getById(item.getCategoryId());
          setmealDto.setCategoryName(category.getName());
          setmealDto.setSort(category.getSort());
          BeanUtils.copyProperties(item,setmealDto);
          return  setmealDto;
      }).collect(Collectors.toList());

      setmealDtoList = setmealDtoList.stream().sorted
              (Comparator.comparingInt(SetmealDto::getSort))
              .collect(Collectors.toList());


      setmealDtoPage.setRecords(setmealDtoList);
      return  R.success(setmealDtoPage);
  }

  @DeleteMapping
    public R<String> deleteSetmeal(@RequestParam List<Long> ids)
  {
      setmealService.removeSetmealWithDish(ids);
      return R.success("套餐删除成功");
  }

  @GetMapping("/list")
    public R<List<Setmeal>> getSetmeal(Setmeal setmeal){
      LambdaQueryWrapper<Setmeal> setmealQuery = new LambdaQueryWrapper<>();
      setmealQuery.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId())
              .eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus())
              .orderByDesc(Setmeal::getUpdateTime);
      List<Setmeal> setmealList = setmealService.list(setmealQuery);
      return  R.success(setmealList);
  }



}
